package b3.widget.national
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;

	public class FreeMonumentItem extends Sprite
	{
		private var buttonFetch:Sprite;
		
		public function FreeMonumentItem(permitIndex:int)
		{
			super();
			var permitConfig:PermitConfig = Config.objConfigs[permitIndex];
			var buildingConfig:BuildingConfig = Config.buildingConfigs[permitConfig.buildingIndex];
			//图片
			var BuildingImage:Class = ResourceHelper.getInstance().getClassByName(buildingConfig.iconImage[0]);
			var buildingImg:Sprite = new BuildingImage();
			buildingImg.mouseEnabled = false;
			buildingImg.width = 50;
			buildingImg.scaleY = buildingImg.scaleX;
			addChild(buildingImg);
			//说明
			var buildingTitleField:TextField = new TextField();
			buildingTitleField.selectable = false;
        	buildingTitleField.autoSize = TextFieldAutoSize.LEFT;
			buildingTitleField.defaultTextFormat = new TextFormat(BFont.fontName, 12, 0x000000);
			buildingTitleField.wordWrap = true;
			buildingTitleField.width = 100;
			buildingTitleField.x = 65;
			buildingTitleField.y = 18; 
			buildingTitleField.text = "每天可免费领取一星纪念碑 2 座";
			addChild(buildingTitleField);
			//另取按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgrNational");
			buttonFetch = new Sprite(); 
			buttonFetch.addChild(new BasicButton(ButtonBgr, "立即领取"));
			buttonFetch.addEventListener(MouseEvent.CLICK, onButtonFetchClick);
			buttonFetch.x = 65;
			buttonFetch.y = 60;
			addChild(buttonFetch);
			var me:Player = PlayerManager.getInstance().me;
			me.addEventListener(ModelEvent.FETCH_NATIONAL_DAILY_PRESENT, onFetchNationalDailyGiftComplete, false, 0, true);
			onRefresh(null);
		}
		
		private function onRefresh(event:ModelEvent):void
		{
			var me:Player = PlayerManager.getInstance().me;
			if (me.fetchNationalGift)
			{
				buttonFetch.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
				var button:BasicButton = buttonFetch.getChildAt(0) as BasicButton;
				button.text = "已经领取";
			}
		}
		
		private function onButtonFetchClick(event:MouseEvent):void
		{
			var me:Player = PlayerManager.getInstance().me;
			if (!me.fetchNationalGift)
			{
				me.fetchNationalDailyPresent();
			}
		}
		
		private function onFetchNationalDailyGiftComplete(event:ModelEvent):void
		{
			onRefresh(event);
			var data:Object = event.data;
			if (data['err'] == 0)
			{
				//提示获取礼物成功
				var awards:Array = new Array();
				for (var str:String in data['awards'])
				{
					var obj:Obj = new Obj();
					obj.loadbyObject(data['awards'][str]);
					awards.push(obj);
				}
				var awardBox:FectchAwardCompleteBox = new FectchAwardCompleteBox(awards);
				awardBox.x = -awardBox.width / 2;
				awardBox.y = -awardBox.height / 2;
				WidgetManager.getInstance().popOutOneByOne(awardBox);
			}
			else
			{
				var popMessage:PopMessage = new PopMessage("今日礼物已经领取过了！", 0xFF0000);
				WidgetManager.getInstance().popOut([popMessage]);	
			}
		}
		
	}
}